import { queryAllRole, saveRoleAdd, saveRoleEdit, queryRoleInfo, saveRoleDel, queryPermissionMap } from '../services/role';

import { message } from 'antd';

export default {
  namespace: 'role',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    permissionMap:[],
    loading: {
      reslist:true,
      add:false,
      info:false,
      permissionMap:true,
    },
  },

  effects: {
    *fetch_role_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{reslist:true}
      })
      const response =yield call(queryAllRole, payload);
      response &&  (
        yield put({
          type: 'appendData',
          payload:response.data,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{reslist:false}
      })
    },

    *action_role_del({ payload }, { call, put }) {
      const response =yield call(saveRoleDel, payload);
    },

    *add_role({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{add:true}
      })
      const response =yield call(saveRoleAdd, payload);
      yield put({
        type: 'changeLoading',
        payload:{add:false}
      })
    },

    *edit_role({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{add:true}
      })
      const response =yield call(saveRoleEdit, payload);
      yield put({
        type: 'changeLoading',
        payload:{add:false}
      })
    },

    *fetch_role_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{info:true}
      })
      const response =yield call(queryRoleInfo, payload);
      response &&  (
        yield put({
          type: 'appendDataDetail',
          payload:response.data
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{info:false}
      })
    },

    *fetch_permission_map({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{permissionMap:true,info:true}
      })
      const response =yield call(queryPermissionMap, payload);
      response &&  (
        yield put({
          type: 'appendPermissionData',
          payload:response.data.list
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{permissionMap:false}
      })
    },



  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendPermissionData(state, action) {
      return {
        ...state,
        permissionMap: action.payload,
      };
    },

    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        permission_map:[],
        detaildata:{},
        loading:{}
      };
    },
  },
};

